package com.beteng.su.betengsu.net.webservice;

import android.content.Context;
import android.util.Log;


import com.beteng.su.betengsu.base.APPConstants;
import com.beteng.su.betengsu.utils.CommonUtils;
import com.beteng.su.betengsu.utils.Md5;
import com.beteng.su.betengsu.utils.StringUtil;
import com.blankj.utilcode.util.EmptyUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SPUtils;

import org.json.JSONException;
import org.json.JSONObject;
import org.ksoap2.serialization.SoapObject;

import java.util.HashMap;


public  class WebService {
    private static final String TAG = "WebService";
    private  String singleParam;
    //参数
    private String paramsStr;
    private HashMap<String, Object> params;
    private String methodName;
    private SoapObject soapObject;
    private String url;
    private String headerData;
    private Context mContext;
    private HashMap<String, Object> paramsData;

    private String keyForHmac = "574584H38Msx80980026QKzbX588Zv0xh95ph8ZG67dj7x69k5091xvm0013";
    // AESUtil加密与解密的密钥，截取商户密钥的前16位
    private String keyForAes = keyForHmac.substring(0, 16);

    public WebService(String url, HashMap<String, Object> params, String methodName, Context context) {
        this(url,null,params,methodName,context);
    }

    public WebService(String url, String singleParam,HashMap<String, Object> params, String methodName, Context context) {
        this.url = url;
        this.singleParam = singleParam;
        this.params = params;
        this.methodName = methodName;
        this.mContext = context;
    }


    public WebService(String serviceIp, String bebusSubmit, Context context) {
        this(serviceIp,null,bebusSubmit,context);
    }


    public WebServiceResultEntity call()  {
        headerData = "";
        paramsData = encryptParams(methodName);
        soapObject = LastestWebServiceUtils.callWebService(
                url, methodName, paramsData, "http://tempuri.org/", headerData);
        if (soapObject != null) {
            String jsonStr = soapObject.getProperty(methodName + "Result")
                    .toString();
            Log.e(TAG,"Resopne:: Method: "+methodName+"Data: "+jsonStr);
            WebServiceResultEntity resultEntity = new WebServiceResultEntity();
            try {

                JSONObject dataObject = new JSONObject(jsonStr);
                try {
                    resultEntity.setResult(jsonStr);
                    resultEntity.setData(dataObject.getString("Data"));
                    if (StringUtil.isEquals(CommonUtils.Method.LOGIN,methodName)) {
                            LogUtils.d("ticket:::"+dataObject.getString("Data"));
                        SPUtils.getInstance().put(APPConstants.USER_TICKET,dataObject.getString("Data"));
                    }

                } catch (JSONException e) {
                    try{
                        resultEntity.setData(dataObject.getJSONArray("Data").toString());
                    }catch (JSONException e2){
                        resultEntity.setData(dataObject.getBoolean("Data"));
                    }

                }
                resultEntity.setStatus(dataObject.getInt("Status"));
                resultEntity.setMessage(dataObject.getString("Message"));


                resultEntity.setSuccess(true);

            } catch (JSONException e) {
                resultEntity.setSuccess(false);
            }
            return resultEntity;
        }
        WebServiceResultEntity resultEntity = new WebServiceResultEntity();
        resultEntity.setSuccess(false);
        return  resultEntity;
    }


    private HashMap<String, Object> encryptParams(String methodName) {
            JSONObject json = new JSONObject();
            HashMap<String,Object> map = new HashMap<>();
        try {
            JSONObject tempJson = null;
            if (params!=null) {
                tempJson = new JSONObject(params);
            }

            if (EmptyUtils.isEmpty(singleParam)){
                json.put("Data",tempJson);

            }else {
                json.put("Data",singleParam);
            }



            String md5 = Md5.getMD5(json.toString());
            map.put("data",json.toString());
            map.put("key",md5);
            map.put("ticket", SPUtils.getInstance().getString(APPConstants.USER_TICKET));

            LogUtils.d("Request:: Method: "+methodName+"Data: "+json.toString());

        } catch (Exception e) {
            e.printStackTrace();
        }

        return map;
    }
    //
    //private HashMap<String, Object> encryptParams() {
    //
    //    HashMap<String,Object> map = new HashMap<>();
    //
    //
    //    map.put("data","{\"Data\":{\"userNo\":\"000533\",\"password\":\"0\",\"SN\":\"安卓\"}}");
    //    map.put("key","e137fc24af6a72146c31ac36ee8933a8");
    //    map.put("ticket",BaseApplication.USERTICKET);
    //
    //
    //    return map;
    //}


}